import { Component } from '@angular/core';
import { IonicPage, NavController, ModalController} from 'ionic-angular';

import { CustomerService } from '../../providers/providers';
import { Utils } from '../../providers/providers';
/**
 * Generated class for the IndexPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-index',
  templateUrl: 'index.html',
})
export class IndexPage {
  private indutryList: any  = [];
  private customerList: any  = [];
  indutryItem: any;
  
  constructor(public navCtrl: NavController,public customerService: CustomerService,
   public utils: Utils, public modalCtrl: ModalController) {

  }

  ionViewDidLoad() {
    this.queryIndutries();
  }

  clickSearch() {
    let searchModal = this.modalCtrl.create('SearchPage');
    searchModal.onDidDismiss(telOrId => {
      if(telOrId){
        this.navCtrl.push('CustomerListPage', {
          tradeTypeId: this.indutryItem.tradId,  
          telOrId: telOrId
        });
      }
    })
    searchModal.present();
  }

  // 选择行业
  selectTradItem(item) {
    this.indutryItem = item;
    this.queryCustomers();
  }  

  // 选择客户类型
  selectCustItem(item) {
    let param = {
      tradeTypeId: this.indutryItem.tradId,  
      customTypeId: item.custId
    };
    this.navCtrl.push('CustomerListPage', param);
  }

  // 查询客户类型
  queryCustomers() {
    let params = {
      tradId: this.indutryItem.tradId
    };
    this.customerService.queryStatByCustomer(params)
    .subscribe((res) => {
      if(res.success) {
        this.customerList = res.data;
      }
    });
  }

   // 查询行业
   queryIndutries() {
    this.utils.showLoading();
    this.customerService.queryStatByIndutry()
    .subscribe((res) => {
      this.utils.hideLoading();
      if(res.success && res.data.length > 0) {
        this.indutryList = res.data;
        this.indutryItem = res.data[0];
        this.queryCustomers();
      } else {
        this.utils.showAlert(res.retMessage);
      }
    });
  }

}
